<div id="tt">
	<a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="saveRolePermit();">保存</a>
</div>
<input type="hidden" id="roleId" name="roleId" value="${RequestParameters['roleId']}" />
<ul id="permitTree" class="ztree"></ul>
<script type="text/javascript">
	var setting = {
		check: {
			enable: true,
			chkboxType: { "Y" : "s", "N" : "ps" },
		},
		data: {
			simpleData: {
				enable: true,
				idKey: 'permit_id',
				pIdKey: 'parent_id',
				rootPId: null
			}
		},
		callback: {
			onClick: zTreeOnClick
		}
	};

	// 节点被点击的事件回调函数
	function zTreeOnClick(event, treeId, treeNode) {
		var treeObj = $.fn.zTree.getZTreeObj("permitTree");
		var node = treeObj.getNodeByTId(treeNode.tId);
		treeObj.checkNode(node, true, true);
	}

	var zTree;
	$(document).ready(function(){
		zTree = $.fn.zTree.init($("#permitTree"), setting, jQuery.parseJSON('${node}'));
		expandNodeLevel(1);
	});

	function expandNodeLevel(nodeLevel){
		var nodes = zTree.getNodes();
		for(var i=0;i<nodes.length;i++){
			if(nodes[i].level<=nodeLevel){
				zTree.expandNode(zTree.getNodeByParam("permit_id", nodes[i].permit_id));
			}

			var cNodes = zTree.transformToArray(nodes[i])
			for(var j=0;j<cNodes.length;j++){
				if(cNodes[j].level<=nodeLevel && nodes[i].level<cNodes[j].level){
					zTree.expandNode(zTree.getNodeByParam("permit_id", cNodes[j].permit_id));
				}
			}
		}
	}

	function saveRolePermit(){
		parent.layer.load(2);
		var treeObj = $.fn.zTree.getZTreeObj("permitTree");
		var nodes = treeObj.getCheckedNodes(true);
		var permitId = '';
		var flag = '';
		for(var i=0;i<nodes.length;i++){
			permitId = permitId + nodes[i].permit_id + ',';
			flag = flag + nodes[i].flag + ',';
		}
		var roleId = $('#roleId').val();

		if (permitId == ''){
			parent.easyui.msgShow('系统提醒', '请选择权限', 3000, 250, 100);
			parent.layer.closeAll('loading');
			return ;
		} else {
			permitId = permitId.substr(0, permitId.lastIndexOf(','));
			flag = flag.substr(0, flag.lastIndexOf(','));
		}

		common.ajax('${app}/sysmgr/role/saveRolePermit.do',
				'roleId='+roleId+'&permitId='+permitId+'&flag='+flag,
				'POST', 'json', false,
				function(result){
					parent.layer.closeAll('loading');
					parent.easyui.msgShow('系统提醒', result.msg, 3000, 250, 100);
				}
		);
	}

</script>